package com.example.pceb.service.Impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.pceb.VO.ApplyInformationVO;
import com.example.pceb.entity.ApplyInformation;
import com.example.pceb.entity.Project;
import com.example.pceb.mapper.ApplyInformationMapper;
import com.example.pceb.mapper.ProjectMapper;
import com.example.pceb.result.Result;
import com.example.pceb.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProjectServiceImpl implements ProjectService {
    @Autowired
    ProjectMapper projectMapper;
    @Autowired
    ApplyInformationMapper applyInformationMapper;
    @Override
    public Result<String> addProject(ApplyInformation applyInformation, Project project) {
        projectMapper.insert(project);
        applyInformation.setProjectId(project.getProject_id());
        applyInformationMapper.insert(applyInformation);
        return Result.success("申报成功");
    }

    @Override
    public Result<List<ApplyInformationVO>> getProjectByUsername(String responsiblePerson) {
        LambdaQueryWrapper<ApplyInformation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApplyInformation::getResponsiblePerson, responsiblePerson);
        List<ApplyInformation> applyInformationList = applyInformationMapper.selectList(queryWrapper);
        List<ApplyInformationVO> applyInformationVOList = new ArrayList<>();
        for (ApplyInformation applyInformation : applyInformationList) {
            ApplyInformationVO applyInformationVO = new ApplyInformationVO();
            applyInformationVO.setApplyInformation(applyInformation);
            applyInformationVO.setProject(projectMapper.selectById(applyInformation.getProjectId()));
            applyInformationVOList.add(applyInformationVO);
        }
        return Result.success(applyInformationVOList);
    }
}
